package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.OriginItemsMapper;
import com.koocloud.electroplatemanage.pojo.AnalysisItems;
import com.koocloud.electroplatemanage.pojo.OriginItems;
import com.koocloud.electroplatemanage.pojo.TypeWorkVo;
import com.koocloud.electroplatemanage.service.OriginItemsService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020/11/17 16:33
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class OriginItemsServiceImpl implements OriginItemsService {

    @Resource
    private OriginItemsMapper originItemsMapper;
    Lock lock = new ReentrantLock();
    /**
     *@Description:通过逐渐查找一条数据
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate selectByPk(String pk) {
        try {
            //新建工种实体
            OriginItems originItems = null;
            originItems = originItemsMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != originItems) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(originItems).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description:通过镀槽编号查询记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate selectByTankNo(String tankNo) {
        try{
            //新建化验项目类
            List<OriginItems> list = new ArrayList<>();
            list = originItemsMapper.selectByTankNo(tankNo);
            if (!list.isEmpty()){
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(list).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        }catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description:通过主键和镀槽编号查找一条数据
     *@Param: [tankNo,originItemList]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate batchInsert(String tankNo,List<OriginItems> originItemsList) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = originItemsMapper.batchInsert(tankNo,originItemsList);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }
    /**
     *@Description:通过镀槽的编号删除数据
     *@Param: [tankNo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate deleteByTankNo(String tankNo) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = originItemsMapper.deleteByTankNo(tankNo);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("删除数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("删除数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }
}
